<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import { computed, reactive, ref } from "vue";
import {
  InsertMemoTemplate,
  UpdateMemoTemplate,
  MemoTemplateVO,
  insertMemoTemplateApi,
  updateMemoTemplateApi
 } from "@/api/biz/strategy/memo";
import { useUserStoreHook } from "@/store/modules/user";
import { ElMessage, FormInstance, FormRules } from "element-plus";

interface Props {
  type: "add" | "update";
  modelValue: boolean;
  row?: MemoTemplateVO;
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

const formData = reactive<InsertMemoTemplate | UpdateMemoTemplate>({
  id: 0,
  name: "",
  desc: "",
  memoType: 1,
  suggestStrategy: 1,
  memoContent: "",
  referenceOpinion: "",
  status: 1
});

const statusList = useUserStoreHook().dictionaryMap["common.status"];

const rules: FormRules = {
  name: [
    {
      required: true,
      message: "备忘录名称不能不为空"
    }
  ],
  desc: [
    {
      required: true,
      message: "备忘录描述不能为空"
    }
  ],
  memoType: [
    {
      required: true,
      message: "备忘录类型不能为空"
    }
  ],
  memoContent: [
    {
      required: true,
      message: "备忘录详情不能为空"
    }
  ],
  referenceOpinion: [
    {
      required: true,
      message: "备忘录关联意见不能为空"
    }
  ],
  status: [
    {
      required: false,
      message: "备忘录状态意见不能为空"
    }
  ]
};
const formRef = ref<FormInstance>();
function handleOpened() {
  if (props.row) {
    Object.assign(formData, props.row);
  } else {
    formRef.value?.resetFields();
  }
}

const loading = ref(false);
async function handleConfirm() {
  try {
    loading.value = true;
    if (props.type === "add") {
      await insertMemoTemplateApi(formData);
    } else if (props.type === "update") {
      await updateMemoTemplateApi(formData as UpdateMemoTemplate);
    }
    ElMessage.info("提交成功");
    visible.value = false;
    emits("success");
  } catch (e) {
    console.error(e);
    ElMessage.error((e as Error)?.message || "提交失败");
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <v-dialog
    show-full-screen
    :fixed-body-height="false"
    use-body-scrolling
    :title="type === 'add' ? '新增平台备忘录' : '更新平台备忘录'"
    v-model="visible"
    :loading="loading"
    @confirm="handleConfirm"
    @cancel="visible = false"
    @opened="handleOpened"
  >
    <el-form :model="formData" label-width="120px" :rules="rules" ref="formRef">
      <el-form-item prop="name" label="备忘录名称" required inline-message>
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item prop="desc" label="备忘录描述" required>
        <el-input type="textarea" v-model="formData.desc" />
      </el-form-item>
      <el-form-item prop="memoType" label="备忘录类型" required>
        <el-input v-model="formData.memoType" />
      </el-form-item>
      <el-form-item prop="suggestStrategy" label="备忘录建议类型" required>
        <el-input v-model="formData.suggestStrategy" />
      </el-form-item>
      <el-form-item prop="memoContent" label="备忘录详情" required>
        <el-input v-model="formData.memoContent" />
      </el-form-item>
      <el-form-item prop="referenceOpinion" label="备忘录关联意见" required>
        <el-input v-model="formData.referenceOpinion" />
      </el-form-item>
      <el-form-item prop="status" label="备忘录状态">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="item in Object.keys(statusList)"
            :key="item"
            :label="statusList[item].value"
            >{{ statusList[item].label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
  </v-dialog>
</template>
